﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    public class OperatorMonthMoney
    {
        public int Id { get; set; }
        /// <summary>
        /// 操作员编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int Month { get; set; }

        public int Year { get; set; }

        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }

        /// <summary>
        /// 得到交易日账户信息
        /// </summary>
        /// <param name="accountMoneys"></param>
        /// <returns></returns>
        public OperatorMonthMoney GetOperatorMonthMoney(int month, int year, int operatorId, DB.DBManager db)
        {
            var query = db.OperatorMonthMoney.Where(a => a.Year == year).Where(a => a.Month == month).Where(a => a.OperatorId == operatorId);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 初始化操作员资金信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void InitOperatorMonthMoney(int month, int year, DB.DBManager db)
        {
            var operators = db.Operators.ToList();
            foreach (Sys.Operator op in operators)
            {
                View.OperatorMonthMoney omm = new View.OperatorMonthMoney();
                omm.Month = month;
                omm.Year = year;
                omm.OperatorId = op.Id;

                List<Data.Month.AccountMonthMoney> moneys = new Data.Month.AccountMonthMoney().GetAccountAndAllChange(op.Id, month, year, db);
                foreach (Data.Month.AccountMonthMoney money in moneys)
                {
                    omm.AppendMargin += money.AppendMargin;
                    omm.AvailableMoney += money.AvailableMoney;
                    omm.BaseMargin += money.BaseMargin;
                    omm.CloseProfit += money.CloseProfit;
                    omm.Commission += money.Commission;
                    omm.DeliveryCommission += money.DeliveryMargin;
                    omm.Deposit += money.Deposit;
                    omm.DynamicMoney += money.DynamicMoney;
                    omm.FrozenCommission += money.FrozenCommission;
                    omm.FrozenMargin += money.FrozenMargin;
                    omm.LongPosition += money.LongPosition;
                    omm.Margin += money.Margin;
                    omm.Mortgage += money.Mortgage;
                    omm.PositionProfit += money.PositionProfit;
                    omm.Premortgage += money.Premortgage;
                    omm.PresetTlePosit += money.PresetTlePosit;
                    omm.RiskRate += money.RiskRate;
                    omm.SettlePosit += money.SettlePosit;
                    omm.ShortPosition += money.ShortPosition;
                    omm.WithDraw += money.WithDraw;
                    omm.WithDrawQuota += money.WithDrawQuota;
                }

                var query = db.OperatorMonthMoney.Where(a => a.Month == month).Where(a => a.Year == month).Where(a => a.OperatorId == op.Id);
                if (query.Count() == 0)
                {
                    db.OperatorMonthMoney.Add(omm);
                }
                else
                {
                    query.First().AppendMargin = omm.AppendMargin;
                    query.First().AvailableMoney = omm.AvailableMoney;
                    query.First().BaseMargin = omm.BaseMargin;
                    query.First().CloseProfit = omm.CloseProfit;
                    query.First().Commission = omm.Commission;
                    query.First().DeliveryCommission = omm.DeliveryMargin;
                    query.First().Deposit = omm.Deposit;
                    query.First().DynamicMoney = omm.DynamicMoney;
                    query.First().FrozenCommission = omm.FrozenCommission;
                    query.First().FrozenMargin = omm.FrozenMargin;
                    query.First().LongPosition = omm.LongPosition;
                    query.First().Margin = omm.Margin;
                    query.First().Mortgage = omm.Mortgage;
                    query.First().PositionProfit = omm.PositionProfit;
                    query.First().Premortgage = omm.Premortgage;
                    query.First().PresetTlePosit = omm.PresetTlePosit;
                    query.First().RiskRate = omm.RiskRate;
                    query.First().SettlePosit = omm.SettlePosit;
                    query.First().ShortPosition = omm.ShortPosition;
                    query.First().WithDraw = omm.WithDraw;
                    query.First().WithDrawQuota = omm.WithDrawQuota;
                }
            }
            db.SaveChanges();
        }

        public void Delete(int year, int month, DB.DBManager db)
        {
            var query = db.OperatorMonthMoney.Where(a => a.Year == year).Where(a => a.Month == month);
            foreach (OperatorMonthMoney m in query)
            {
                db.OperatorMonthMoney.Remove(m);
            }
            db.SaveChanges();
        }
    }
}
